#include "ElaWidget.h"
#include "ElaDialog.h"
#include <QApplication>
#include "ElaApplication.h"
#include <QVBoxLayout>
#include <QHBoxLayout>
#include <QLabel>
#include <QPushButton>
#include <QTextEdit>
#include <QSpinBox>
#include <QMessageBox>
#include <QDateTime>
#include <QLineEdit>
#include <QComboBox>
#include <QTimer>


// 数据输入对话框
class DataInputDialog : public ElaDialog
{
    Q_OBJECT
public:
    DataInputDialog(QWidget* parent = nullptr) : ElaDialog(parent)
    {
        setWindowTitle("数据输入");
        setWindowButtonFlags(ElaAppBarType::CloseButtonHint);
        resize(400, 300);
        
        auto* layout = new QVBoxLayout(this);
        
        // 输入字段
        auto* nameLayout = new QHBoxLayout();
        nameLayout->addWidget(new QLabel("姓名:"));
        m_nameEdit = new QLineEdit();
        nameLayout->addWidget(m_nameEdit);
        
        auto* ageLayout = new QHBoxLayout();
        ageLayout->addWidget(new QLabel("年龄:"));
        m_ageSpin = new QSpinBox();
        m_ageSpin->setRange(1, 100);
        ageLayout->addWidget(m_ageSpin);
        
        auto* genderLayout = new QHBoxLayout();
        genderLayout->addWidget(new QLabel("性别:"));
        m_genderCombo = new QComboBox();
        m_genderCombo->addItems({"男", "女", "其他"});
        genderLayout->addWidget(m_genderCombo);
        
        m_notesEdit = new QTextEdit();
        m_notesEdit->setPlaceholderText("备注信息...");
        
        // 按钮
        auto* buttonLayout = new QHBoxLayout();
        auto* submitButton = new QPushButton("提交");
        auto* clearButton = new QPushButton("清空");
        auto* cancelButton = new QPushButton("取消");
        
        buttonLayout->addWidget(submitButton);
        buttonLayout->addWidget(clearButton);
        buttonLayout->addWidget(cancelButton);
        
        layout->addLayout(nameLayout);
        layout->addLayout(ageLayout);
        layout->addLayout(genderLayout);
        layout->addWidget(new QLabel("备注:"));
        layout->addWidget(m_notesEdit, 1);
        layout->addLayout(buttonLayout);
        
        // 连接信号
        connect(submitButton, &QPushButton::clicked, this, &DataInputDialog::onSubmit);
        connect(clearButton, &QPushButton::clicked, this, &DataInputDialog::onClear);
        connect(cancelButton, &QPushButton::clicked, this, &DataInputDialog::close);
        connect(this, &DataInputDialog::closeButtonClicked, this, &DataInputDialog::close);
    }
    
signals:
    void dataSubmitted(const QString& name, int age, const QString& gender, const QString& notes);

private Q_SLOTS:
    void onSubmit()
    {
        if (m_nameEdit->text().isEmpty()) {
            QMessageBox::warning(this, "警告", "请输入姓名!");
            return;
        }
        
        emit dataSubmitted(m_nameEdit->text(), m_ageSpin->value(), 
                          m_genderCombo->currentText(), m_notesEdit->toPlainText());
        close();
    }
    
    void onClear()
    {
        m_nameEdit->clear();
        m_ageSpin->setValue(0);
        m_genderCombo->setCurrentIndex(0);
        m_notesEdit->clear();
    }

private:
    QLineEdit* m_nameEdit;
    QSpinBox* m_ageSpin;
    QComboBox* m_genderCombo;
    QTextEdit* m_notesEdit;
};

// 数据显示窗口
class DataDisplayWindow : public ElaWidget
{
    Q_OBJECT
public:
    DataDisplayWindow(QWidget* parent = nullptr) : ElaWidget(parent)
    {
        setWindowTitle("数据管理系统");
        resize(700, 500);
        
            setWindowButtonFlags(ElaAppBarType::StayTopButtonHint | ElaAppBarType::MinimizeButtonHint | ElaAppBarType::MaximizeButtonHint | ElaAppBarType::CloseButtonHint | ElaAppBarType::ThemeChangeButtonHint | ElaAppBarType::NavigationButtonHint);

        
        setupUI();
        
        QTimer::singleShot(100, this, &DataDisplayWindow::moveToCenter);
    }

private:
    QTextEdit* m_displayArea;
    QPushButton* m_inputButton;
    QPushButton* m_clearButton;
    
    void setupUI()
    {
        auto* layout = new QVBoxLayout(this);
        
        // 标题
        auto* titleLabel = new QLabel("数据记录系统");
        titleLabel->setStyleSheet("font-size: 20px; font-weight: bold; margin: 15px;");
        titleLabel->setAlignment(Qt::AlignCenter);
        
        // 显示区域
        m_displayArea = new QTextEdit();
        m_displayArea->setReadOnly(true);
        m_displayArea->setPlaceholderText("暂无数据记录...");
        
        // 按钮区域
        auto* buttonLayout = new QHBoxLayout();
        m_inputButton = new QPushButton("新增数据");
        m_clearButton = new QPushButton("清空记录");
        
        buttonLayout->addWidget(m_inputButton);
        buttonLayout->addStretch();
        buttonLayout->addWidget(m_clearButton);
        
        layout->addWidget(titleLabel);
        layout->addWidget(m_displayArea, 1);
        layout->addLayout(buttonLayout);
        
        // 连接信号
        connect(m_inputButton, &QPushButton::clicked, this, &DataDisplayWindow::showInputDialog);
        connect(m_clearButton, &QPushButton::clicked, m_displayArea, &QTextEdit::clear);
        connect(this, &DataDisplayWindow::closeButtonClicked, this, &DataDisplayWindow::close);
    }
    
private Q_SLOTS:
    void showInputDialog()
    {
        auto* dialog = new DataInputDialog(this);
        connect(dialog, &DataInputDialog::dataSubmitted, this, &DataDisplayWindow::onDataSubmitted);
        dialog->show();
        dialog->moveToCenter();
    }
    
    void onDataSubmitted(const QString& name, int age, const QString& gender, const QString& notes)
    {
        QString record = QString("【新记录】\n"
                                "姓名: %1\n"
                                "年龄: %2\n" 
                                "性别: %3\n"
                                "备注: %4\n"
                                "时间: %5\n"
                                "------------------------\n")
                        .arg(name)
                        .arg(age)
                        .arg(gender)
                        .arg(notes.isEmpty() ? "无" : notes)
                        .arg(QDateTime::currentDateTime().toString("yyyy-MM-dd hh:mm:ss"));
        
        m_displayArea->append(record);
        
        QMessageBox::information(this, "成功", "数据已保存!");
    }
};

int main(int argc, char *argv[])
{
#if (QT_VERSION < QT_VERSION_CHECK(6, 0, 0))
    QGuiApplication::setAttribute(Qt::AA_UseHighDpiPixmaps);  // 启用高 DPI (界面布局和几何)
#if (QT_VERSION >= QT_VERSION_CHECK(5, 14, 0))
    QGuiApplication::setAttribute(Qt::AA_EnableHighDpiScaling);                                               //  启用自动高 DPI 缩放 (图标和图片)
    QGuiApplication::setHighDpiScaleFactorRoundingPolicy(Qt::HighDpiScaleFactorRoundingPolicy::PassThrough);  // 使用精确的缩放因子，不进行四舍五入
#else
    // 根据实际屏幕缩放比例更改
    qputenv("QT_SCALE_FACTOR", "1.5");
#endif
#endif

    QApplication app(argc, argv);

    eApp->init();  // 初始化资源、字体等
    // eApp->setWindowDisplayMode(ElaApplicationType::ElaMica);
    
    DataDisplayWindow window;
    window.show();
    
    return app.exec();
}

#include "main_multiple_dialogs.moc"